package com.power.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.power.constant.ProductConstants;
import com.power.domain.Category;
import com.power.ex.handle.BussinessException;
import com.power.mapper.CategoryMapper;
import com.power.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author dingchunlin
* @description 针对表【category(产品类目)】的数据库操作Service实现
* @createDate 2024-07-10 15:12:13
*/
@Service
@CacheConfig(cacheNames = "com.power.service.impl.CategoryServiceImpl")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询所有产品类目列表
     * @return 产品类目列表
     */
    @Override
    @Cacheable(key = ProductConstants.ALL_CATEGORY_LIST_KEY)
    public List<Category> queryAllCategoryList() {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Category::getSeq);
        List<Category> categoryList = categoryMapper.selectList(lambdaQueryWrapper);
        return categoryList;
    }

    /**
     * 查询一级产品类目列表
     *
     * @return 一级产品类目列表
     */
    @Override
    @Cacheable(key = ProductConstants.FIRST_CATEGORY_LIST_KEY)
    public List<Category> queryFirstCategoryList() {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getParentId, 0);
        lambdaQueryWrapper.eq(Category::getStatus, 1);
        lambdaQueryWrapper.orderByAsc(Category::getSeq);
        List<Category> categoryList = categoryMapper.selectList(lambdaQueryWrapper);
        return categoryList;
    }

    /**
     * 新增商品类目
     * @param category
     * @return
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.FIRST_CATEGORY_LIST_KEY)
    })
    public Boolean saveCategory(Category category) {
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        return categoryMapper.insert(category) > 0;
    }

    /**
     * 修改商品类目
     * @param category
     * @return
     */
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.FIRST_CATEGORY_LIST_KEY)
    })
    @Override
    public Boolean modifyCategory(Category category) {
        //获取现在的pid 用于判断是否需要修改子类目
        Long parentId = category.getParentId();
        //首先查询出原来的数据
        Category dbCategory = categoryMapper.selectById(category.getCategoryId());
        // 获取之前的商品级别
        Long beforeParentId = dbCategory.getParentId();
        // 判断商品类目级别是否发生变化
        // 1-》2 ：之前pid为0，现在不为0，说明从一级类目变为二级类目
        if (beforeParentId == 0 && null != parentId && parentId!= 0) {
            // 查询当前类目是否存在子类目
            List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                    .eq(Category::getParentId, category.getCategoryId()));
            if (CollectionUtils.isNotEmpty(categoryList) && categoryList.size() > 0){
                // 存在子类目，不能修改
                throw new BussinessException("该类目下存在子类目，不能修改");
            }
        }
        // 2-》1 ：之前pid不为0，现在为null，说明从二级类目变为一级类目
        if (beforeParentId!= 0 && parentId == null) {
            category.setParentId(0L);
        }
        return categoryMapper.updateById(category) > 0;
    }

    /**
     * 根据类目ID删除商品类目
     *
     * @param categoryId 类目ID
     * @return 删除结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.FIRST_CATEGORY_LIST_KEY)
    })
    public Boolean removeCategoryById(Long categoryId) {
        //首先判断是否存在子类目
        List<Category> childrenCategoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
               .eq(Category::getParentId, categoryId));
        if (CollectionUtils.isNotEmpty(childrenCategoryList) && childrenCategoryList.size() > 0){
            // 存在子类目，不能删除
            throw new BussinessException("该类目下存在子类目，不能删除");
        }
        //删除当前类目
        return categoryMapper.deleteById(categoryId) > 0;
    }

    /**
     * 微信小程序端获取商品类目
     * @param parentId 父类目ID
     * @return 商品类目列表
     */
    @Override
    public List<Category> queryCategoryList(Long parentId) {
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, parentId)
                .eq(Category::getStatus, 1)
                .orderByAsc(Category::getSeq));
        return categoryList;
    }

    /**
     * 根据父类目ID获取商品类目列表
     * @param parentId 父类目ID
     * @return 商品类目列表
     */
    @Override
    public List<Category> queryCategoryListByParentId(Long parentId) {
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, parentId)
                .eq(Category::getStatus, 1)
                .orderByAsc(Category::getSeq));
        return categoryList;
    }

}




